﻿using System;
using System.Configuration;
using System.Web;

namespace EasyLibrary.AR.Classes
{
    internal class ConnectionString
    {
        public static string GetUsingHostname()
        {
            HttpContext ctx = HttpContext.Current;
            if (ctx == null)
                throw new InvalidOperationException(
                    "Cannot get the ConnectionString based on the web request since you are not in a web request.");

            string hostname = ctx.Request.Url.DnsSafeHost;

            ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings[hostname.ToLower()];
            if (connectionString == null)
            {
                ctx.Response.Redirect("http://www.easysoftware.pt/easyfrota.aspx", true);
                return null;
                //throw new InvalidOperationException("Cannot get the ConnectionString based on the web request for the hostname '"  + hostname + "'");
            }

            return connectionString.ConnectionString;
        }
    }
}